var faceUtil = {
  width: 150, //图片宽度
  height: 150, //图片高度
  isOpen: false, //是否开启摄像头
  vvv:null,
  openVideo: function(id) { //打开摄像头
    var mythis = this;
    $("#" + id + "").empty();
    let vedioComp = "<video id='myVideo' width='" + this.width + "px' height='" + this.height +
      "px' autoplay='autoplay' style='margin-top: 0px'></video>" +
      "<canvas id='myCavans' width='" + this.width + "px' height='" + this.height +
      "px' style='display: none'></canvas>";
    $("#" + id + "").append(vedioComp);
    let constraints = {
      video: {
        width: mythis.width,
        height: mythis.height
      },
      audio: true
    };
    //获得video摄像头区域
    let video = document.getElementById("myVideo");

     var self = this;
    let promise = navigator.mediaDevices.getUserMedia(constraints);
    promise.then(function(MediaStream) {
      // console.log("11111111")
      // console.log(MediaStream.getTracks())
       self.vvv = MediaStream.getTracks()[1]
      video.srcObject = MediaStream;
      video.play();



    });
    this.isOpen = true;
  },
  getDecode: function() { //获取图片decode吗
    if (this.isOpen) {
      let myVideo = document.getElementById("myVideo");
      let myCavans = document.getElementById("myCavans");
      let ctx = myCavans.getContext('2d');
      ctx.drawImage(myVideo, 0, 0, this.width, this.height);
      var decode = myCavans.toDataURL();
      return decode;
    } else {
      alert("没有开启摄像头");

    }

  },
  closess:function(){
    // console.log("22222222")
    // console.log(this.vvv)
    this.vvv.stop();
  }
}


export default faceUtil;
